﻿using System;
using System;
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class _CombBlade3
    {
        public static void CombBlade3(Dictionary<string, Object> data)//螺纹梳刀片3
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour");//轮廓层
            Layer dimensioning = new Layer("dimensioning");//尺寸层
            Layer refin = new Layer("refin");//基准线/内轮廓/虚线
            Layer center = new Layer("center");//中心线
            Layer textlayer = new Layer("textlayer");//文本层
            Layer hatching = new Layer("hatching");//填充层
            Layer additionalline = new Layer("additionalline");//附加线
            Layer cutting = new Layer("cutting");//切削层
            fun.SetLayer(dxf);

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double offset = 10;//偏移量


            double scale = Convert.ToDouble(data["scale"]); //放大系数



            double insl = Convert.ToDouble(data["insl"]) * scale;
            double tp = Convert.ToDouble(data["tp"]) * scale;//齿距
            double s1 = Convert.ToDouble(data["s1"]) * scale;
            double popt = Convert.ToDouble(data["popt"]) * scale;//齿高
            double w1 = Convert.ToDouble(data["w1"]) * scale;//刀片宽度
            double d = insl / tp;
            int i = (int)d;//齿数取整
            double a = Convert.ToDouble(data["a"]);//倾斜角度
            var hand = "1";
            try
            {
                hand = data["hand"].ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine("[hand] not found");
            }




            //作图
            if (hand.Equals("0"))
            {
                //平齿
                Vector2 v1 = new Vector2(0, 0);
                Vector2 v2 = new Vector2(v1.X, v1.Y - w1 / 1.745);
                Vector2 v3 = new Vector2(v2.X - insl / 32.135, v2.Y - w1 / 7.656);
                Vector2 v4 = new Vector2(v3.X, v1.Y - w1 / 7.656);
                Vector2 v5 = new Vector2(v1.X, v1.Y - w1 / 12.376);
                Vector2 v6 = new Vector2(v3.X - insl / 12.655, v3.Y - w1 / 8.897);
                Vector2 v10 = new Vector2(v1.X - insl, v1.Y);
                Vector2 v11 = new Vector2(v10.X, v5.Y);
                Vector2 v12 = new Vector2(v11.X + insl / 32.135, v4.Y);
                Vector2 v9 = new Vector2(v10.X, v2.Y);
                Vector2 v8 = new Vector2(v12.X, v3.Y);
                Vector2 v7 = new Vector2(v8.X + insl / 12.655, v6.Y);
                Vector2 v13 = new Vector2(v7.X + insl / 39.72, v1.Y - w1 + popt);
                Vector2 v14 = new Vector2(v6.X - insl / 39.72, v13.Y);
                Vector2 v15 = new Vector2(v1.X - insl / 2, v4.Y - (v4.Y - v14.Y) / 2);
                Vector2 v16 = new Vector2(v1.X - (insl - i * tp) / 2, v1.Y);
                Vector2 v17 = new Vector2(v16.X - tp / 2, v16.Y + popt);
                Vector2 v18 = new Vector2(v16.X - tp, v16.Y);
                Vector2 v19 = new Vector2(v17.X - tp, v17.Y);
                Vector2 a1 = new Vector2(v1.X, v16.Y + popt / 4);
                Vector2 a2 = new Vector2(a1.X - insl, a1.Y);
                Vector2 a3 = new Vector2(v1.X, v17.Y - popt / 8);
                Vector2 a4 = new Vector2(a2.X, a3.Y);
                Vector2 a5 = fun.Inters(v16, v17, a1, a2);
                Vector2 a6 = fun.Inters(v16, v17, a3, a4);
                Vector2 a7 = fun.Inters(v17, v18, a3, a4);
                Vector2 a8 = fun.Inters(v17, v18, a1, a2);
                Vector2 a9 = fun.Inters(v19, v18, a1, a2);
                Vector2 a10 = new Vector2(a6.X - tp, a6.Y);
                Vector2 a11 = new Vector2(a7.X - tp * i, a7.Y);
                Vector2 v20 = new Vector2(a8.X - tp * (i - 1), a5.Y);
                //侧视图
                Vector2 v21 = new Vector2(v1.X + insl / 3.178, v6.Y);
                Vector2 v22 = new Vector2(v21.X + s1 / 2, v14.Y);
                Vector2 v23 = new Vector2(v21.X + s1, v21.Y);
                Vector2 v24 = new Vector2(v23.X, v4.Y);
                Vector2 v25 = new Vector2(v24.X, a6.Y);
                Vector2 v26 = new Vector2(v21.X, v25.Y);
                Vector2 v27 = new Vector2(v26.X, v24.Y);
                Vector2 v28 = new Vector2(v21.X + s1 / 3.15, v15.Y - w1 / 5.14);
                Vector2 v29 = new Vector2(v23.X - s1 / 3.15, v28.Y);
                Vector2 v30 = new Vector2(v29.X, v15.Y + w1 / 5.14);
                Vector2 v31 = new Vector2(v28.X, v30.Y);
                Vector2 v32 = new Vector2(v21.X, v15.Y + w1 / 3.666);
                Vector2 v33 = new Vector2(v21.X, v15.Y - w1 / 3.666);
                Vector2 v34 = new Vector2(v23.X, v33.Y);
                Vector2 v35 = new Vector2(v34.X, v32.Y);



                LwPolyline line1 = new LwPolyline();
                line1.Vertexes.Add(new LwPolylineVertex(a5));
                line1.Vertexes.Add(new LwPolylineVertex(v1));
                line1.Vertexes.Add(new LwPolylineVertex(v2));
                line1.Vertexes.Add(new LwPolylineVertex(v3));
                line1.Vertexes.Add(new LwPolylineVertex(v6));
                line1.Vertexes.Add(new LwPolylineVertex(v7));
                line1.Vertexes.Add(new LwPolylineVertex(v8));
                line1.Vertexes.Add(new LwPolylineVertex(v9));
                line1.Vertexes.Add(new LwPolylineVertex(v10));
                line1.Vertexes.Add(new LwPolylineVertex(v20));
                line1.Vertexes[3].Bulge = Math.Tan(-56 * pi / 180 / 4);
                line1.Vertexes[5].Bulge = Math.Tan(-56 * pi / 180 / 4);
                fun.PolyFill(dxf, line1, 0.1, contour);

                fun.drawcircle(dxf, v15, w1 / 5.14, 0.1, contour);

                fun.drawcircle(dxf, v15, w1 / 3.666, 0.1, contour);

                LwPolyline line2 = new LwPolyline();
                line2.Vertexes.Add(new LwPolylineVertex(v6));
                line2.Vertexes.Add(new LwPolylineVertex(v14));
                line2.Vertexes.Add(new LwPolylineVertex(v13));
                line2.Vertexes.Add(new LwPolylineVertex(v7));
                fun.PolyFill(dxf, line2, 0.1, contour);

                LwPolyline line3 = new LwPolyline();
                line3.Vertexes.Add(new LwPolylineVertex(v11));
                line3.Vertexes.Add(new LwPolylineVertex(v12));
                line3.Vertexes.Add(new LwPolylineVertex(v8));
                line3.Vertexes.Add(new LwPolylineVertex(v12));
                line3.Vertexes.Add(new LwPolylineVertex(v4));
                line3.Vertexes.Add(new LwPolylineVertex(v3));
                line3.Vertexes.Add(new LwPolylineVertex(v4));
                line3.Vertexes.Add(new LwPolylineVertex(v5));
                fun.PolyFill(dxf, line3, 0.1, contour);

                LwPolyline line4 = new LwPolyline();
                line4.Vertexes.Add(new LwPolylineVertex(a5));
                line4.Vertexes.Add(new LwPolylineVertex(a6));
                line4.Vertexes.Add(new LwPolylineVertex(a7));
                line4.Vertexes.Add(new LwPolylineVertex(a8));
                fun.PolyFill(dxf, line4, 0.1, contour);

                fun.aray1(dxf, line4, 180, tp, i, 0.1, contour);

                LwPolyline line5 = new LwPolyline();
                line5.Vertexes.Add(new LwPolylineVertex(a8));
                line5.Vertexes.Add(new LwPolylineVertex(a9));
                fun.PolyFill(dxf, line5, 0.1, contour);

                fun.aray1(dxf, line5, 180, tp, i - 1, 0.1, contour);


                Arc arc1 = fun.DrawArc3(v21, v22, v23);
                fun.drawBArc(dxf, arc1, 0.1, contour);

                LwPolyline line6 = new LwPolyline();
                line6.Vertexes.Add(new LwPolylineVertex(v23));
                line6.Vertexes.Add(new LwPolylineVertex(v24));
                line6.Vertexes.Add(new LwPolylineVertex(v25));
                line6.Vertexes.Add(new LwPolylineVertex(v26));
                line6.Vertexes.Add(new LwPolylineVertex(v27));
                line6.Vertexes.Add(new LwPolylineVertex(v21));
                line6.Vertexes.Add(new LwPolylineVertex(v33));
                line6.Vertexes.Add(new LwPolylineVertex(v28));
                line6.Vertexes.Add(new LwPolylineVertex(v29));
                line6.Vertexes.Add(new LwPolylineVertex(v30));
                line6.Vertexes.Add(new LwPolylineVertex(v31));
                line6.Vertexes.Add(new LwPolylineVertex(v32));
                line6.Vertexes.Add(new LwPolylineVertex(v31));
                line6.Vertexes.Add(new LwPolylineVertex(v28));
                line6.Vertexes[1].Bulge = Math.Tan(-72 * pi / 180 / 4);
                line6.Vertexes[3].Bulge = Math.Tan(-72 * pi / 180 / 4);
                fun.PolyFill(dxf, line6, 0.1, contour);

                LwPolyline line7 = new LwPolyline();
                line7.Vertexes.Add(new LwPolylineVertex(v34));
                line7.Vertexes.Add(new LwPolylineVertex(v29));
                line7.Vertexes.Add(new LwPolylineVertex(v30));
                line7.Vertexes.Add(new LwPolylineVertex(v35));
                fun.PolyFill(dxf, line7, 0.1, contour);




                //标注

                double height = 2;
                double arrowsize = 2;
                double exline = 2;


                fun.dimrot1(dxf, a6, a10, 0, offset, 1, height, arrowsize, exline, "P=" + tp / scale, "", "");

                fun.dimrot1(dxf, v9, v2, 180, offset +Math.Abs ( v13.Y-v2.Y), 1, height, arrowsize, exline, "A=" + insl / scale, "", "");

                fun.dimrot1(dxf, a6, v13, -90, offset + Math.Abs(a6.X - v13.X) / 2-a6.X, 1, height, arrowsize, exline, "H=" + w1 / scale, "", "");

                fun.dimrot1(dxf, v21, v23, 180, offset+Math.Abs (v13.Y-v21.Y), 1, height, arrowsize, exline, "T=" + s1/scale, "", "");



            }
            else
            {
                //斜齿
                Vector2 v10 = new Vector2(0, 0);
                Vector2 v9 = new Vector2(v10.X, v10.Y - w1 / 1.745);
                Vector2 v8 = new Vector2(v9.X + insl / 32.135, v9.Y - w1 / 7.656);
                Vector2 v12 = new Vector2(v8.X, v10.Y - w1 / 7.656);
                Vector2 v11 = new Vector2(v10.X, v10.Y - w1 / 12.376);
                Vector2 v7 = new Vector2(v8.X + insl / 12.655, v8.Y - w1 / 8.897);
                Vector2 v1 = fun.Polar(v10, -a * pi / 180, insl * Math.Cos(a * pi / 180));
                Vector2 v2 = new Vector2(v1.X, v9.Y);
                Vector2 v3 = new Vector2(v2.X - insl / 32.135, v8.Y);
                Vector2 v5 = new Vector2(v1.X, v1.Y - w1 / 12.376);
                Vector2 v4 = new Vector2(v3.X, v1.Y - w1 / 7.656);
                Vector2 v6 = new Vector2(v3.X - insl / 12.655, v7.Y);
                Vector2 v13 = new Vector2(v7.X + insl / 39.72, v10.Y - w1 + popt);
                Vector2 v14 = new Vector2(v6.X - insl / 39.72, v13.Y);
                Vector2 v15 = new Vector2(v10.X + insl / 2, v12.Y - (v12.Y - v14.Y) / 2);
                Vector2 v16 = new Vector2(v10.X + (insl - i * tp) / 2, v10.Y);
                Vector2 v17 = new Vector2(v16.X + tp / 2, v16.Y + popt);
                Vector2 v18 = new Vector2(v16.X + tp, v16.Y);
                Vector2 v19 = new Vector2(v17.X + tp, v17.Y);
                Vector2 a1 = new Vector2(v10.X, v16.Y + popt / 4);
                Vector2 a2 = fun.Polar(a1, -a * pi / 180, insl / Math.Cos(a * pi / 180));
                Vector2 a3 = new Vector2(a1.X, v17.Y - popt / 8);
                Vector2 a4 = fun.Polar(a3, -a * pi / 180, insl / Math.Cos(a * pi / 180));
                Vector2 a5 = fun.Inters(v16, v17, a1, a2);
                Vector2 a6 = fun.Inters(v16, v17, a3, a4);
                Vector2 a7 = fun.Inters(v17, v18, a3, a4);
                Vector2 a8 = fun.Inters(v17, v18, a1, a2);
                Vector2 a9 = fun.Inters(v19, v18, a1, a2);
                Vector2 a10 = fun.Polar(a6, -a * pi / 180, tp / Math.Cos(a * pi / 180));
                Vector2 v20 = fun.Polar(a8, -a * pi / 180, tp * (i - 1) / Math.Cos(a * pi / 180));
                //侧视图
                Vector2 v21 = new Vector2(v1.X + insl / 3.178, v6.Y);
                Vector2 v22 = new Vector2(v21.X + s1 / 2, v14.Y);
                Vector2 v23 = new Vector2(v21.X + s1, v21.Y);
                Vector2 v24 = new Vector2(v23.X, v4.Y);
                Vector2 v25 = new Vector2(v24.X, a6.Y);
                Vector2 v26 = new Vector2(v21.X, v25.Y);
                Vector2 v27 = new Vector2(v26.X, v24.Y);
                Vector2 v28 = new Vector2(v21.X + s1 / 3.15, v15.Y - w1 / 5.14);
                Vector2 v29 = new Vector2(v23.X - s1 / 3.15, v28.Y);
                Vector2 v30 = new Vector2(v29.X, v15.Y + w1 / 5.14);
                Vector2 v31 = new Vector2(v28.X, v30.Y);
                Vector2 v32 = new Vector2(v21.X, v15.Y + w1 / 3.666);
                Vector2 v33 = new Vector2(v21.X, v15.Y - w1 / 3.666);
                Vector2 v34 = new Vector2(v23.X, v33.Y);
                Vector2 v35 = new Vector2(v34.X, v32.Y);



                LwPolyline line1 = new LwPolyline();
                line1.Vertexes.Add(new LwPolylineVertex(v20));
                line1.Vertexes.Add(new LwPolylineVertex(v1));
                line1.Vertexes.Add(new LwPolylineVertex(v2));
                line1.Vertexes.Add(new LwPolylineVertex(v3));
                line1.Vertexes.Add(new LwPolylineVertex(v6));
                line1.Vertexes.Add(new LwPolylineVertex(v7));
                line1.Vertexes.Add(new LwPolylineVertex(v8));
                line1.Vertexes.Add(new LwPolylineVertex(v9));
                line1.Vertexes.Add(new LwPolylineVertex(v10));
                line1.Vertexes.Add(new LwPolylineVertex(a5));
                line1.Vertexes[3].Bulge = Math.Tan(-56 * pi / 180 / 4);
                line1.Vertexes[5].Bulge = Math.Tan(-56 * pi / 180 / 4);
                fun.PolyFill(dxf, line1, 0.1, contour);

                fun.drawcircle(dxf, v15, (v4.Y - v14.Y) / 3.459, 0.1, contour);

                fun.drawcircle(dxf, v15, (v4.Y - v14.Y) / 2.424, 0.1, contour);

                LwPolyline line2 = new LwPolyline();
                line2.Vertexes.Add(new LwPolylineVertex(v6));
                line2.Vertexes.Add(new LwPolylineVertex(v14));
                line2.Vertexes.Add(new LwPolylineVertex(v13));
                line2.Vertexes.Add(new LwPolylineVertex(v7));
                fun.PolyFill(dxf, line2, 0.1, contour);

                LwPolyline line3 = new LwPolyline();
                line3.Vertexes.Add(new LwPolylineVertex(v11));
                line3.Vertexes.Add(new LwPolylineVertex(v12));
                line3.Vertexes.Add(new LwPolylineVertex(v8));
                line3.Vertexes.Add(new LwPolylineVertex(v12));
                line3.Vertexes.Add(new LwPolylineVertex(v4));
                line3.Vertexes.Add(new LwPolylineVertex(v3));
                line3.Vertexes.Add(new LwPolylineVertex(v4));
                line3.Vertexes.Add(new LwPolylineVertex(v5));
                fun.PolyFill(dxf, line3, 0.1, contour);

                LwPolyline line4 = new LwPolyline();
                line4.Vertexes.Add(new LwPolylineVertex(a5));
                line4.Vertexes.Add(new LwPolylineVertex(a6));
                line4.Vertexes.Add(new LwPolylineVertex(a7));
                line4.Vertexes.Add(new LwPolylineVertex(a8));
                fun.PolyFill(dxf, line4, 0.1, contour);

                fun.aray1(dxf, line4, -a, tp / Math.Cos(a * pi / 180), i, 0.1, contour);

                LwPolyline line5 = new LwPolyline();
                line5.Vertexes.Add(new LwPolylineVertex(a8));
                line5.Vertexes.Add(new LwPolylineVertex(a9));
                fun.PolyFill(dxf, line5, 0.1, contour);

                fun.aray1(dxf, line5, -a, tp / Math.Cos(a * pi / 180), i - 1, 0.1, contour);


                Arc arc1 = fun.DrawArc3(v21, v22, v23);
                fun.drawBArc(dxf, arc1, 0.1, contour);

                LwPolyline line6 = new LwPolyline();
                line6.Vertexes.Add(new LwPolylineVertex(v23));
                line6.Vertexes.Add(new LwPolylineVertex(v24));
                line6.Vertexes.Add(new LwPolylineVertex(v25));
                line6.Vertexes.Add(new LwPolylineVertex(v26));
                line6.Vertexes.Add(new LwPolylineVertex(v27));
                line6.Vertexes.Add(new LwPolylineVertex(v21));
                line6.Vertexes.Add(new LwPolylineVertex(v33));
                line6.Vertexes.Add(new LwPolylineVertex(v28));
                line6.Vertexes.Add(new LwPolylineVertex(v29));
                line6.Vertexes.Add(new LwPolylineVertex(v30));
                line6.Vertexes.Add(new LwPolylineVertex(v31));
                line6.Vertexes.Add(new LwPolylineVertex(v32));
                line6.Vertexes.Add(new LwPolylineVertex(v31));
                line6.Vertexes.Add(new LwPolylineVertex(v28));
                line6.Vertexes[1].Bulge = Math.Tan(-72 * pi / 180 / 4);
                line6.Vertexes[3].Bulge = Math.Tan(-72 * pi / 180 / 4);
                fun.PolyFill(dxf, line6, 0.1, contour);

                LwPolyline line7 = new LwPolyline();
                line7.Vertexes.Add(new LwPolylineVertex(v34));
                line7.Vertexes.Add(new LwPolylineVertex(v29));
                line7.Vertexes.Add(new LwPolylineVertex(v30));
                line7.Vertexes.Add(new LwPolylineVertex(v35));
                fun.PolyFill(dxf, line7, 0.1, contour);



                //标注

                double height = 2;
                double arrowsize = 2;
                double exline = 2;


                fun.dimrot1(dxf, a6, a10, 0, offset, 1, height, arrowsize, exline, "P=" + tp / scale, "", "");

                fun.dimrot1(dxf, v9, v2, 180, offset + Math.Abs(v13.Y - v2.Y), 1, height, arrowsize, exline, "A=" + insl / scale, "", "");

                fun.dimrot1(dxf, a6, v13, -90, offset + Math.Abs(a6.X - v13.X) / 2 - a6.X, 1, height, arrowsize, exline, "H=" + w1 / scale, "", "");

                fun.dimrot1(dxf, v21, v23, 180, offset + Math.Abs(v13.Y - v21.Y), 1, height, arrowsize, exline, "T=" + s1 / scale, "", "");






            }


            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);

            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            fun.SetLayer(dxf);
            fun.SetByLayer(dxf);

            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}